<template>
  <div class="nickname">
    <van-nav-bar
  title="昵称"
  left-text="取消"
  right-text="完成"
  @click-left="$emit('close')"
  @click-right="onClickRight"
/>
  <div class="field">
    <van-field
  v-model.trim="message"
  rows="2"
  autosize
  type="textarea"
  maxlength="7"
  show-word-limit
/>
  </div>
  </div>
</template>

<script>
import { editUserinfo } from '@/api/user'
export default {
  name: 'nickName',
  components: {},
  props: {
    value: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      message: this.value
    }
  },
  computed: {},
  watch: {},
  created () {

  },
  mounted () {

  },
  methods: {
    async onClickRight() {
      // 加载提示
      this.$toast.loading({
        message: '更新中...',
        forbidClick: true,
        // 设置转动时间，为0就无限转动
        duration: 0
      })
      try {
        await editUserinfo({ name: this.message })
        this.$emit('input', this.message)
        this.$toast.success('更新昵称成功')
      } catch (err) {
        if (err.response && err.response.status === 409) {
          this.$toast.fail('用户昵称已存在')
        } else {
          this.$toast.fail('更新昵称失败')
        }
      }
    }
  }
}
</script>

<style scoped lang="less">
.nickname {
  .field {
    padding: 15px;
  }
}
</style>
